﻿using TMPro;
using UnityEngine;
using UnityEngine.UI;

namespace HEFramework
{
    public static class LegacyLotteryUISystem
    {
        [EntitySystem]
        public class LegacyLotteryUIAwakeSystem : AwakeSystem<LegacyLotteryUI>
        {
            protected override void Awake(LegacyLotteryUI _self)
            {
                _self.AddChild<UnityObjectComponent>();
                _self.Open();
            }
        }


        [EntitySystem]
        public class LegacyLotteryUIUpdateSystem : UpdateSystem<LegacyLotteryUI>
        {
            protected override void Update(LegacyLotteryUI _self)
            {
                _self.Update();
            }
        }


        [EntitySystem]
        public class LegacyLotteryUIDestroySystem : DestroySystem<LegacyLotteryUI>
        {
            protected override void Destroy(LegacyLotteryUI _self)
            {
                _self.Close();
            }
        }


        public static void Open(this LegacyLotteryUI _self)
        {
            var uc = _self.GetSceneChild<UIComponent>();
            if (uc == null)
            {
                return;
            }

            uc.PushUIAsync(eUIFlag.LegacyLottery, _ =>
            {
                _self.UIWindowBehaviour = _;
                _self.Bind();
                _self.State = 0;
                _self.Update(true);
            });
        }

        public static void Bind(this LegacyLotteryUI _self)
        {
            if (_self.UIWindowBehaviour == null)
            {
                return;
            }

            _self.UIWindowBehaviour.GetButton("Button_Trigger").RegisterUpEvent(() =>
            {
                if (_self.Lottery())
                {
                    _self.OpenPanel();
                }
            });
            _self.UIWindowBehaviour.GetButton("Button_Back").RegisterUpEvent(_self.ClosePanel);
            _self.UIWindowBehaviour.GetButton("Button_Abandon").RegisterUpEvent(() =>
            {
                if (_self.Abandon())
                {
                    _self.ClosePanel();
                }
            });
            for (int i = 0; i < 3; i++)
            {
                int index = i;
                _self.UIWindowBehaviour.GetButton($"Button_LegacyLotteryItem{i}").RegisterUpEvent(() =>
                {
                    if (_self.Select(index))
                    {
                        _self.ClosePanel();
                    }
                });
            }
        }

        public static void Update(this LegacyLotteryUI _self, bool _isNew = false)
        {
            if (_self.UIWindowBehaviour == null || _self.UIWindowBehaviour.State == 0)
            {
                return;
            }

            var rc = _self.GetSceneChild<ResourcesComponent>();
            var dc = _self.GetSceneChild<DataTableComponent>();
            var uc = _self.GetChild<UnityObjectComponent>();
            var legacyLottery = _self.GetSceneChild<LegacyLottery>();
            if (rc == null || dc == null || uc == null || legacyLottery == null)
            {
                return;
            }

            var language_table = dc.LoadDataTable<DRLanguage>();

            var button_back = _self.UIWindowBehaviour.GetButton("Button_Back").GetGameObject();
            var button_trigger = _self.UIWindowBehaviour.GetButton("Button_Trigger").GetGameObject();
            var transform_panel = _self.UIWindowBehaviour.GetTransform("Transform_Panel").gameObject;

            //只要能抽就展示
            if (legacyLottery.IsCanLottery())
            {
                button_trigger.SetActive(true);
                _self.UIWindowBehaviour.GetTMPText("TMPText_Count").SetText(legacyLottery.LotteryCount.ToString());
            }
            else
            {
                button_trigger.SetActive(false);
            }

            //面板
            if (_self.State == 0)
            {
                button_back.SetActive(false);
                transform_panel.gameObject.SetActive(false);
            }
            else if (_self.State == 1)
            {
                button_back.SetActive(true);
                transform_panel.SetActive(true);

                if (_isNew)
                {
                    //丢弃按钮处理
                    var abandon_button = _self.UIWindowBehaviour.GetButton("Button_Abandon");
                    var name = abandon_button.GetTransform().GetChild(0);
                    uc.Get<TMP_Text>(name.gameObject).text = language_table.GetData(134).Text;

                    string message = "{0:+0;-0}{1}";
                    int count = 3;
                    //加载图片
                    var Lotteries = legacyLottery.Lotteries;
                    for (int i = 0; i < count; i++)
                    {
                        var button = _self.UIWindowBehaviour.GetButton($"Button_LegacyLotteryItem{i}");
                        if (i >= Lotteries.Count)
                        {
                            button.GetGameObject().SetActive(false);
                        }
                        else
                        {
                            button.GetGameObject().SetActive(true);
                            var data = dc.LoadDataTable<DRLegacy>().GetData(Lotteries[i]);
                            var button_transform = button.GetTransform();

                            //质量修改
                            var quality_transform = button_transform.GetChild(0);
                            int child_count = quality_transform.childCount;
                            int type = data.QualityType;
                            for (int j = 0; j < child_count; j++)
                            {
                                if (type == j)
                                {
                                    quality_transform.GetChild(j).gameObject.SetActive(true);
                                }
                                else
                                {
                                    quality_transform.GetChild(j).gameObject.SetActive(false);
                                }
                            }

                            //图标

                            //名字
                            uc.Get<TMP_Text>(button_transform.GetChild(2).gameObject).text = data.Name;

                            //元素
                            var element_transform = button_transform.GetChild(3);
                            for (int j = 0; j < element_transform.childCount; j++)
                            {
                                element_transform.GetChild(j).gameObject.SetActive(false);
                            }

                            for (int j = 0; j < data.ElementIDs.Length; j++)
                            {
                                switch (data.ElementIDs[j])
                                {
                                    case (int)eElement.Gold:
                                        element_transform.GetChild(0).gameObject.SetActive(true);
                                        break;
                                    case (int)eElement.Wood:
                                        element_transform.GetChild(1).gameObject.SetActive(true);
                                        break;
                                    case (int)eElement.Water:
                                        element_transform.GetChild(2).gameObject.SetActive(true);
                                        break;
                                    case (int)eElement.Fire:
                                        element_transform.GetChild(3).gameObject.SetActive(true);
                                        break;
                                    case (int)eElement.Soil:
                                        element_transform.GetChild(4).gameObject.SetActive(true);
                                        break;
                                }
                            }

                            //信息
                            var reward_table = dc.LoadDataTable<DRReward>();
                            _self.StringBuilder.Clear();
                            for (int j = 0; j < data.RewardIDs.Length; j++)
                            {
                                _self.StringBuilder.AppendLine(string.Format(message, data.RewardValues[j], reward_table.GetData(data.RewardIDs[j]).Name));
                            }

                            uc.Get<TMP_Text>(button_transform.GetChild(4).gameObject).text = _self.StringBuilder.ToString();
                            _self.StringBuilder.Clear();
                        }
                    }
                }
            }
        }

        public static void Close(this LegacyLotteryUI _self)
        {
            if (_self.UIWindowBehaviour == null)
            {
                return;
            }

            _self.UIWindowBehaviour.Hide();
            _self.UIWindowBehaviour = null;
        }

        public static void OpenPanel(this LegacyLotteryUI _self)
        {
            _self.State = 1;
            _self.Update(true);
        }

        public static void ClosePanel(this LegacyLotteryUI _self)
        {
            _self.State = 0;
        }


        public static bool Lottery(this LegacyLotteryUI _self)
        {
            var legacyLottery = _self.GetSceneChild<LegacyLottery>();
            if (legacyLottery == null)
            {
                return false;
            }

            if (legacyLottery.IsHaveCache())
            {
                return true;
            }

            legacyLottery.Lottery();
            return true;
        }

        public static bool Abandon(this LegacyLotteryUI _self)
        {
            var legacyLottery = _self.GetSceneChild<LegacyLottery>();
            if (legacyLottery == null)
            {
                return false;
            }

            legacyLottery.Abandon();
            return true;
        }

        public static bool Select(this LegacyLotteryUI _self, int _index)
        {
            var dc = _self.GetSceneChild<DataTableComponent>();
            var legacyLottery = _self.GetSceneChild<LegacyLottery>();
            var reporterUI = _self.GetSceneChild<ReporterUI>();
            var gameUI = _self.GetSceneChild<GameUI>();
            if (dc == null || legacyLottery == null || reporterUI == null || gameUI == null)
            {
                return false;
            }

            var language_table = dc.LoadDataTable<DRLanguage>();
            int legacyId = legacyLottery.Lotteries[_index];
            if (!legacyLottery.IsCanSelect(legacyId))
            {
                reporterUI.Reporter(language_table.GetData(135).Text);
                return false;
            }

            //选择
            legacyLottery.Select(legacyId);
            return true;
        }
    }
}